package com.kuizii.generator.util.generator;

import com.kuizii.generator.util.DateUtils;
import com.kuizii.generator.util.RRException;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 * 自动生成biz模块 //TODO 还没生成测试代码
 * </p>
 *
 * @author nuanyang
 * Email
 * created by 2018/6/20
 */
public class GeneratorBizUtils {

    private static List<String> getOnceTemplates() {
        List<String> templates = new ArrayList<>();
        templates.add("templates/biz/pom/bizpom.xml.vm");
//        templates.add("templates/biz/convert/convert-package-info.java.vm");
//        templates.add("templates/biz/constant/constant-package-info.java.vm");
//        templates.add("templates/biz/config/config-package-info.java.vm");
//        templates.add("templates/biz/dao/Mapper.java.vm");
//        templates.add("templates/biz/dao/Mapper.xml.vm");
//        templates.add("templates/biz/entity/Entity.java.vm");
//        templates.add("templates/biz/data/service/DataService.java.vm");
//        templates.add("templates/biz/data/service/impl/DataServiceImpl.java.vm");
//        templates.add("templates/biz/service/Controller.java.vm");
//        templates.add("templates/biz/service/service-package-info.java.vm");
        return templates;
    }


    /**
     * 生成非数据库表代码
     *
     * @param zip
     */
    public static void generatorOnceCode(ZipOutputStream zip) {
        Configuration config = GeneratorConfig.getConfig();
        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        map.put("mainPath", GeneratorConfig.getProjectPackage(GeneratorConfig.MODULE_API));
        map.put("moduleName", GeneratorConfig.getModuleName().replace("-", "."));
        map.put("author", config.getString("author"));
        map.put("email", config.getString("email"));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        map.put("artifactId", GeneratorConfig.getProjectNameNotSlash(GeneratorConfig.MODULE_BIZ));
        map.put("bizArtifactId", GeneratorConfig.getProjectNameNotSlash(GeneratorConfig.MODULE_BIZ));
        map.put("coreArtifactId", GeneratorConfig.getProjectNameNotSlash(GeneratorConfig.MODULE_CORE));
        map.put("description", config.getString("description"));
        map.put("version", config.getString("version"));
        map.put("moduleType", GeneratorConfig.MODULE_BIZ);
        map.put("package", config.getString("package"));
        map.put("artifactIdParent", GeneratorConfig.getProjectNameNotSlash(GeneratorConfig.MODULE_API));
        map.put("parentArtifactId", GeneratorConfig.getModuleParent() + GeneratorConfig.getModuleName());
        map.put("sql-type", config.getString("sql-type"));
        map.put("rpc-type", config.getString("rpc-type"));
        List<String> templates = getOnceTemplates();
        for (String template : templates) {
            map.put("template", template);
            VelocityContext context = new VelocityContext(map);
            //渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, "UTF-8");
            tpl.merge(context, sw);
            try {
                //添加到zip
                zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getOnceFileName(template))));
                IOUtils.write(sw.toString(), zip, "UTF-8");
                IOUtils.closeQuietly(sw);
                zip.closeEntry();
            } catch (IOException e) {
                throw new RRException("渲染模板失败", e);
            }catch (Exception e){
//                throw new RRException("渲染模板失败", e);
            }
        }
    }

    /**
     * 获取模板对应名称
     *
     * @param template
     * @return
     */
    private static String getOnceFileName(String template) {
        if (template.contains("bizpom.xml.vm")) {
            return GeneratorConfig.getPomRoute(GeneratorConfig.MODULE_BIZ) + "pom.xml";
        }
        if (template.contains("convert-package-info.java.vm")) {
            return GeneratorConfig.getProjectJavaRoute(GeneratorConfig.MODULE_BIZ) + File.separator + "convert" + File.separator + "convert-package-info.java";
        }
        if (template.contains("constant-package-info.java.vm")) {
            return GeneratorConfig.getProjectJavaRoute(GeneratorConfig.MODULE_BIZ) + File.separator + "constant" + File.separator + "constant-package-info.java";
        }
        if (template.contains("config-package-info.java.vm")) {
            return GeneratorConfig.getProjectJavaRoute(GeneratorConfig.MODULE_BIZ) + File.separator + "config" + File.separator + "config-package-info.java";
        }
        if (template.contains("service-package-info.java.vm")) {
            return GeneratorConfig.getProjectJavaRoute(GeneratorConfig.MODULE_BIZ) + File.separator + "service" + File.separator + "service-package-info.java";
        }
        return null;
    }
}
